package cn.like.backEnd.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.like.backEnd.auth.AuthManager;
import cn.like.backEnd.auth.CaptchaManager;
import cn.like.base.BaseController;
import cn.like.base.support.result.Rest;
import cn.like.base.support.exception.code.ExceptionCode;
import cn.like.common.dto.LoginRequestDto;
import cn.like.common.dto.SignInRequestDto;
import cn.like.log.annotation.SysLog;
import cn.like.satoken.jwt.JwtManager;
import io.swagger.annotations.*;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;


/**
 * desc: 认证 控制器 <br>
 * details:
 * <pre>
 * 	   1.登录
 * 	   2.注册
 * 	   3.生成验证码
 * 	</pre>
 *
 * @author like 980650920@qq.com
 * @date 2021-07-08 14:08:55
 * @see BaseController
 */
@Api(tags = "认证 API")
@RestController
@Validated
public class AuthController extends BaseController {

    private final CaptchaManager captchaManager;
    private final AuthManager authManager;
    private final JwtManager jwtUtil;

    public AuthController(CaptchaManager captchaManager, AuthManager authManager, JwtManager jwtUtil) {
        this.captchaManager = captchaManager;
        this.authManager = authManager;
        this.jwtUtil = jwtUtil;
    }

    @GetMapping("auth/hello")
    @SaCheckRole("ADMIN")
    @SysLog(value = "hello接口测试", recordRequestParam = true)
    public Rest<?> hello(){
        return Rest.of(jwtUtil.getUser());
    }

    @ApiOperation(value = "登录")
    @PostMapping("auth/login")
    public Rest<?> login(@RequestBody @Validated LoginRequestDto loginParamDto) {

        // 检验验证码 密码
        if (!captchaManager.valid(loginParamDto.getKey(), loginParamDto.getCaptcha()))
            return super.failed(ExceptionCode.CAPTCHA_IS_NO_VALID);
        if (authManager.checkIsNotEqPassword(loginParamDto.getPassword(), loginParamDto.getRePassword()))
            return super.failed(ExceptionCode.RE_PASSWORD_IS_NO_VALID);

        return authManager.login(loginParamDto.getAccount(), loginParamDto.getPassword());
    }

    @ApiOperation("注册")
    @PostMapping("auth/signUp")
    public Rest<?> signUp(@RequestBody @Valid SignInRequestDto signInRequestDto) {

        // 检验验证码 密码
        if (!captchaManager.valid(signInRequestDto.getKey(), signInRequestDto.getCaptcha()))
            return super.failed(ExceptionCode.CAPTCHA_IS_NO_VALID);
        if (authManager.checkIsNotEqPassword(signInRequestDto.getPassword(), signInRequestDto.getRePassword()))
            return super.failed(ExceptionCode.RE_PASSWORD_IS_NO_VALID);

        return authManager.signUp(signInRequestDto);
    }

    @ApiOperation("生成验证码")
    @GetMapping(value = "auth/createCaptcha/{key}", produces = MediaType.IMAGE_PNG_VALUE)
    public void createCaptcha(@PathVariable @ApiParam(required = true,name = "key",value = "需要生成的用户的随机key") String key,
                              HttpServletResponse response) throws Exception {
        captchaManager.create(key, response);
    }

    @ApiOperation("校验验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Key", value = "需要生成的用户的随机key", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "captcha", value = "需要校验的验证码", required = true, dataTypeClass = String.class),
    })
    @GetMapping(value = "auth/validCaptcha/{key}/{captcha}")
    public Rest<?> validCaptcha(@PathVariable String captcha, @PathVariable String key) {
        return Rest.of(captchaManager.valid(key, captcha));
    }

}
